precision mediump float;

uniform sampler2D u_TextureUnit;
uniform vec3 u_LightPos;
uniform vec4 u_LightAmb;
uniform vec4 u_LightDiff;
uniform vec4 u_LightSpec;

varying vec2 v_TextureCoordinates;
varying vec4 v_Normal;
varying vec3 v_N;
varying vec3 v_Position;

void main()
{
    gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);

    //światło
    //model Phonga
    vec3 lightVector = normalize(u_LightPos - v_Position);
    vec3 E = normalize(-v_Position);
    vec3 R = normalize(-reflect(lightVector,v_N));

    //ambient
    vec4 Iamb = u_LightAmb;

    //diffuse
    vec4 Idiff = u_LightDiff * max(dot(v_N, lightVector), 0.0);
    Idiff = clamp(Idiff, 0.0, 1.0);

    //spectacular
    vec4 Ispec = u_LightSpec * pow(max(dot(R,E),0.0),10.0);
    Ispec = clamp(Ispec, 0.0, 1.0);
    gl_FragColor += Iamb + Idiff + Ispec;
}
